Everything Totally Explained


Ask & we'll explain, totally!
Surrogate key
Totally Explained


  NEW! All the latest news in the worlds of computer gaming, entertainment, the environment,  
finance, health, politics, science, stocks & shares, technology and much, much, more.  


View this entry using RSS

Everything about Surrogate Key totally explained

A surrogate key in a database is a unique identifier for either an entity in the modeled world or an object in the database. The surrogate key is not derived from application data.

Definition

There appear to be two definitions of a surrogate in the literature. We shall call these surrogate (1) and surrogate (2):

Surrogate (1) : This definition is based on that given by Hall, Owlett and Todd (1976). Here a surrogate represents an entity in the outside world. The surrogate is internally generated by the system but is nevertheless visible by the user or application. ; Surrogate (2) : This definition is based on that given by Wieringa and de Jung (1991). Here a surrogate represents an object in the database itself. The surrogate is internally generated by the system and is invisible to the user or application.
   We shall adopt the surrogate (1) definition throughout this article largely because it's more data model rather than storage model oriented. See Date (1998).
   An important distinction exists between a surrogate and a primary key, depending on whether the database is a current database or a temporal database. A current database stores only currently valid data, therefore there's a one-to-one correspondence between a surrogate in the modelled world and the primary key of some object in the database; in this case the surrogate may be used as a primary key, resulting in the term surrogate key. However, in a temporal database there's a many-to-one relationship between primary keys and the surrogate. Since there may be several objects in the database corresponding to a single surrogate, we can't use the surrogate as a primary key; another attribute is required, in addition to the surrogate, to uniquely identify each object.
   Although Hall et alia (1976) say nothing about this, other authors have argued that a surrogate should have the following constraints:

  • the value is unique system-wide, hence never reused;
  • the value is system generated;
  • the value isn't manipulable by the user or application;
  • the value contains no semantic meaning;
  • the value isn't visible to the user or application;
  • the value isn't composed of several values from different domains.

Surrogates in practice

In a current database, the surrogate key can be the primary key, generated by the database management system and not derived from any application data in the database. The only significance of the surrogate key is to act as the primary key. It is also possible that the surrogate key exists in addition to the database-generated uuid, for example a HR number for each employee besides the UUID of each employee.
   A surrogate key is frequently a sequential number (for example a Sybase or SQL Server "identity column", a PostgreSQL serial, an Oracle SEQUENCE or a column defined with AUTO_INCREMENT in MySQL) but doesn't have to be. Having the key independent of all other columns insulates the database relationships from changes in data values or database design (making the database more agile) and guarantees uniqueness.
   In a temporal database, it's necessary to distinguish between the surrogate key and the primary key. Typically, every row would have both a primary key and a surrogate key. The primary key identifies the unique row in the database, the surrogate key identifies the unique entity in the modelled world; these two keys are not the same. For example, table Staff may contain two rows for "John Smith", one row when he was employed between 1990 and 1999, another row when he was employed between 2001 and 2006. The surrogate key is identical (non-unique) in both rows however the primary key will be unique.
   Some database designers use surrogate keys religiously regardless of the suitability of other candidate keys, while others will use a key already present in the data, if there's one.
   A surrogate may also be called a
  • surrogate key,
  • entity identifier,
  • system-generated key,
  • database sequence number,
  • synthetic key,
  • technical key, or
  • arbitrary unique identifier. Some of these terms describe the way of generating new surrogate values rather than the nature of the surrogate concept.
       Here are some possible candidates for generating surrogates:
  • Universally Unique Identifiers (UUIDs)
  • Globally Unique Identifiers (GUIDs)
  • Object Identifiers (OIDs)
  • Sybase or SQL Server identity column
  • Oracle SEQUENCE
  • PostgreSQL serial
  • MySQL AUTO_INCREMENT
  • AutoNumber data type in Microsoft Access

    Advantages of Surrogate Keys

    Immutability

    Surrogate keys don't change while the row exists. This has two advantages:
  • Database applications won't lose their "handle" on the row because the data changes;
  • Many database systems don't support cascading updates of keys across foreign keys of related tables. This results in difficulty in modifying the primary key data.

    Flexibility for changing requirements

    Because of changing requirements, the attributes that uniquely identify an entity might change. In that case, the attribute(s) initially chosen as the natural key will no longer be a suitable natural key.
       Example: An employee ID is chosen as the natural key of an employee DB. Because of a merger with another company, new employees from the merged company must be inserted, who have conflicting IDs (as their IDs were independently generated when the companies were separate).
       In these cases, generally a new attribute must be added to the natural key (for example an attribute "original_company"). With a surrogate key, only the table that defines the surrogate key must be changed. With natural keys, all tables that use the natural key will have to change.
       More generally, in some problem domains it's simply not clear what might be a suitable natural key. Surrogate keys avoid problems from choosing a natural key that later turns out to be incorrect.

    Performance

    Often surrogate keys are composed of a compact data type, such as four-byte integers. This allows the database to query faster than it could multiple columns.

    Compatibility

    Several database application development systems, drivers, and object-relational mapping systems, such as Ruby on Rails or Hibernate (Java), depend on the use of integer or GUID surrogate keys in order to support database-system-agnostic operations and object-to-row mapping.

    Disadvantages of Surrogate Keys

    Disassociation

    Because the surrogate key is completely unrelated to the data of the row to which it's attached, the key is disassociated from that row. Disassociated keys are unnatural to the application's world, resulting in an additional level of indirection from which to audit.

    Query Optimization

    Relational databases assume a unique index is applied to a table's primary key. The unique index serves two purposes: 1) to enforce entity integrity—primary key data must be unique across rows—and 2) to quickly search for rows queried. Since surrogate keys replace a table's identifying attributes—the natural key—and since the identifying attributes are likely to be those queried, then the query optimizer is forced to perform a full table scan when fulfilling likely queries. The remedy to the full table scan is to apply a (non-unique) index on each of the identifying attributes. However, these additional indexes will take up disk space, slow down inserts, and slow down deletes.

    Normalization

    The presence of a surrogate key can result in the database administrator forgetting to establish, or accidentally removing, a secondary unique index on the natural key of the table. Without a unique index on the natural key, duplicate rows are likely to appear and are difficult to identify.

    Business Process Modeling

    Because surrogate keys are unnatural, flaws can appear when modeling the business requirements. Business requirements, relying on the natural key, then need to be translated to the surrogate key.

    Inadvertent Disclosure

    Proprietary information may be leaked if sequential key generators are used. By subtracting a previously generated sequential key from a recently generated sequential key, one could learn the number of rows inserted during that time period. This could expose, for example, the number of transactions or new accounts per period. The solution to the inadvertent disclosure problem is to generate a random primary key. However, a randomly generated primary key must be queried before assigned to prevent duplication and cause an insert rejection.

    Inadvertent Assumptions

    Sequentially generated surrogate keys create the illusion that events with a higher primary key value occurred after events with a lower primary key value. This illusion would appear when an event is missed during the normal data entry process and is, instead, inserted after subsequent events were previously inserted. The solution to the inadvertent assumption problem is to generate a random primary key. However, a randomly generated primary key must be queried before assigned to prevent duplication and cause an insert rejection.

    Further Information

    Get more info on 'Surrogate Key'.


    External Link Exchanges

    Do you know how hard it is to get a link from a large encyclopaedia? Well we're different and will prove it. To get a link from us just add the following HTML to your site on a relevant page:

      <a href="http://surrogate_key.totallyexplained.com">Surrogate key Totally Explained</a>

    Then simply click through this link from your web page. Our crawlers will verify your link, extract the title of your web page and instantly add a link back to it. If you like you can remove the words Totally Explained and embed the link in article text.
       As long as your link remains in place, we'll keep our link to you right here. Please play fair - our crawlers are watching. Your site must be closely related to this one's topic. Any kind of spamming, dubious practises or removing the link will result in your link from us being dropped and, potentially, your whole site being banned.



  • Copyright © 2007-8 totallyexplained.com | Licensed under the GNU Free Documentation License | Site Map
    This article contains text from the Wikipedia article Surrogate key (History) and is released under the GFDL | RSS Version